<template>
  <div class="account-on-sale-page">
    <Back-top></Back-top>
    <Nav active-name="account-on-sale"/>
    <div class="breadcrumb-holder flex-row-v-center">
      <div class="item">首页</div>
      <img class="arrow" src="~assets/img/icon-right-arrow.png"/>
      <div class="item active">在售账号</div>
    </div>
    <div class="sec-01">
      <div class="search-box">
        <div class="search-item flex-row" v-for="tag in tagList" :key="tag.id">
          <div class="key">{{ tag.name }}</div>
          <div class="val flex-1 flex-row">
            <div class="tag" :class="{'active': item.selected}" v-for="item in tag.gameTagInfoList" :key="item.id"
                 @click="selectTag(tag, item)">{{ item.name }}
            </div>
          </div>
        </div>
      </div>
      <div class="range-search-box flex-row">
        <div class="asc-desc-box flex-row-v-center">
          <div class="item flex-row-v-center" @click="onClickSort('price')">
            <div class="name">价格</div>
            <div class="icon-box flex-col-v-center" v-if="priceSort < 0">
              <img src="~assets/img/icon-up-inactive.png"/>
              <img src="~assets/img/icon-down-inactive.png"/>
            </div>
            <div class="icon-box flex-col-v-center" v-else-if="priceSort === 0">
              <img src="~assets/img/icon-up-active.png"/>
              <img src="~assets/img/icon-down-inactive.png"/>
            </div>
            <div class="icon-box flex-col-v-center" v-else-if="priceSort === 1">
              <img src="~assets/img/icon-up-inactive.png"/>
              <img src="~assets/img/icon-down-active.png"/>
            </div>
          </div>
          <div class="item flex-row-v-center" @click="onClickSort('hot')">
            <div class="name">热度</div>
            <div class="icon-box flex-col-v-center" v-if="hotSort < 0">
              <img src="~assets/img/icon-up-inactive.png"/>
              <img src="~assets/img/icon-down-inactive.png"/>
            </div>
            <div class="icon-box flex-col-v-center" v-else-if="hotSort === 0">
              <img src="~assets/img/icon-up-active.png"/>
              <img src="~assets/img/icon-down-inactive.png"/>
            </div>
            <div class="icon-box flex-col-v-center" v-else-if="hotSort === 1">
              <img src="~assets/img/icon-up-inactive.png"/>
              <img src="~assets/img/icon-down-active.png"/>
            </div>
          </div>
          <div class="item flex-row-v-center" @click="onClickSort('s1')">
            <div class="name">魅力</div>
            <div class="icon-box flex-col-v-center" v-if="s1Sort < 0">
              <img src="~assets/img/icon-up-inactive.png"/>
              <img src="~assets/img/icon-down-inactive.png"/>
            </div>
            <div class="icon-box flex-col-v-center" v-else-if="s1Sort === 0">
              <img src="~assets/img/icon-up-active.png"/>
              <img src="~assets/img/icon-down-inactive.png"/>
            </div>
            <div class="icon-box flex-col-v-center" v-else-if="s1Sort === 1">
              <img src="~assets/img/icon-up-inactive.png"/>
              <img src="~assets/img/icon-down-active.png"/>
            </div>
          </div>
          <div class="item flex-row-v-center" @click="onClickSort('s2')">
            <div class="name">功力</div>
            <div class="icon-box flex-col-v-center" v-if="s2Sort < 0">
              <img src="~assets/img/icon-up-inactive.png"/>
              <img src="~assets/img/icon-down-inactive.png"/>
            </div>
            <div class="icon-box flex-col-v-center" v-else-if="s2Sort === 0">
              <img src="~assets/img/icon-up-active.png"/>
              <img src="~assets/img/icon-down-inactive.png"/>
            </div>
            <div class="icon-box flex-col-v-center" v-else-if="s2Sort === 1">
              <img src="~assets/img/icon-up-inactive.png"/>
              <img src="~assets/img/icon-down-active.png"/>
            </div>
          </div>
        </div>
        <div class="range-box flex-row-v-center">
          <div class="title">价格区间：</div>
          <div class="min-max-box flex-row-v-center">
            <input class="input" v-model="minPrice" placeholder="最低价"/>
            <div class="split">-</div>
            <input class="input" v-model="maxPrice" placeholder="最高价"/>
          </div>
        </div>
        <div class="range-box flex-row-v-center">
          <div class="title">功力区间：</div>
          <div class="min-max-box flex-row-v-center">
            <input class="input" v-model="minS2" placeholder="最低功力"/>
            <div class="split">-</div>
            <input class="input" v-model="maxS2" placeholder="最高功力"/>
          </div>
        </div>
        <div class="range-box flex-row-v-center">
          <div class="title">魅力区间：</div>
          <div class="min-max-box flex-row-v-center">
            <input class="input" v-model="minS1" placeholder="最低魅力"/>
            <div class="split">-</div>
            <input class="input" v-model="maxS1" placeholder="最高魅力"/>
          </div>
        </div>
        <div class="time-range-box flex-row-v-center">
          <div class="title">上架时间：</div>
          <div class="min-max-box flex-row-v-center">
            <DatePicker placeholder="开始时间" v-model="launchStartTime" format="yyyy年MM月dd日" style="width: 150px"></DatePicker>
            <div class="split">-</div>
            <DatePicker placeholder="结束时间" v-model="launchEndTime" format="yyyy年MM月dd日" style="width: 150px"></DatePicker>
          </div>
        </div>
        <div class="btn" @click="onClickSearch">筛选</div>
      </div>
      <div class="content-holder" v-if="accountList.length > 0">
        <div class="flex-row-v-center" v-for="list in accountList">
          <div class="card-box" v-for="item in list.val" @click="goToAccountInfoPage(item.id)">
            <div class="img-box" v-if="item.id">
              <img :src="item.img" :alt="item.title"/>
              <div class="hot-box flex-row-v-center">
                <img src="~assets/img/icon-hot.png">
                {{ item.hot }}
              </div>
            </div>
            <div class="content-box" v-if="item.id">
              <div class="name-txt">
                {{ item.title }}
              </div>
              <div class="line"></div>
              <div class="price-time-box flex-row-space-between">
                <div class="price-box flex-row-v-center">
                  <div class="unit">¥</div>
                  {{ item.price | moneyFormat }}
                </div>
                <div class="time-box flex-row-v-center">
                  <img src="~assets/img/icon-time.png">{{ item.launch_time }}
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="page flex-center">
          <Page :total="total" :page-size="pageSize" show-total @on-change="pageChange"/>
        </div>
      </div>
      <div class="empty-holder flex-center" v-else>
        <div class="empty-box flex-col-v-center">
          <img src="~assets/img/icon-empty-search.png">
          <div class="txt">未找到相关游戏</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Core from "~/core/core";

export default {
  data() {
    return {
      keyList: [],
      tagList: [],
      tmpList: [],
      gameTagInfoList: [],

      title: '',
      accountList: [],
      currentPage: 1,
      total: 0,
      pageSize: 10,

      hotSort: -1,
      priceSort: -1,
      s1Sort: -1,
      s2Sort: -1,

      minPrice: '',
      maxPrice: '',
      minS1: '',
      maxS1: '',
      minS2: '',
      maxS2: '',
      launchStartTime: '',
      launchEndTime: ''
    }
  },
  mounted() {
    this.title = this.$route.query.title ? this.$route.query.title : ''
    this.getGameTagList(-1, list => {
      list.forEach(v => {
        if (v.flag) {
          v.gameTagInfoList.forEach(t => {
            this.tmpList.push({
              idx: v.id,
              id: t.id,
              name: t.name,
              parentId: t.parentId,
              selected: t.selected
            })
          })
          if (v.name !== "筛选") {
            v.gameTagInfoList = [{
              id: 0,
              name: '全部',
              selected: true,
              parentId: 0,
              map: v.id
            }]
          }
        }
      })
      this.tagList = list
    })
    this.getAccountList()
  },
  methods: {
    getGameTagList(parentId, cb) {
      Core.Api.GameTag.listWithTag({parentId: parentId})
        .then(res => {
          let list = res.list
          list.forEach(v => {
            let flag = false
            v.gameTagInfoList.forEach(t => {
              t.selected = false
              if (t.parentId > 0) {
                flag = true
                v.key = t.parentId
              }
            })
            v.flag = flag
            if (v.name !== "筛选") {
              v.gameTagInfoList.unshift({
                id: 0,
                name: '全部',
                selected: true,
                parentId: 0,
                map: v.id
              })
            }
          })
          list.forEach(v => {
            if (v.key) {
              for (let m = 0; m < list.length; m++) {
                for (let n = 0; n < list[m].gameTagInfoList.length; n++) {
                  if (list[m].gameTagInfoList[n].id === v.key) {
                    this.keyList.push({
                      key: list[m].id,
                      val: v.key
                    })
                    break
                  }
                }
              }
            }
          })
          if (cb) {
            cb(list)
          }
        })
        .catch(err => {
          this.$Message.error(err.msg);
        })
    },
    getAccountList() {
      let data = {
        title: this.title,
        gameTagInfoList: this.gameTagInfoList.sort((a, b) => a - b),
        hotSort: this.hotSort,
        priceSort: this.priceSort,
        s1Sort: this.s1Sort,
        s2Sort: this.s2Sort,
        minPrice: Number(this.minPrice).toFixed(2) * 100,
        maxPrice: Number(this.maxPrice).toFixed(2) * 100,
        minS1: Number(this.minS1).toFixed(2) * 100,
        maxS1: Number(this.maxS1).toFixed(2) * 100,
        minS2: Number(this.minS2).toFixed(2) * 100,
        maxS2: Number(this.maxS2).toFixed(2) * 100,
        page: this.currentPage
      }

      if (this.launchStartTime) {
        data.minLaunchTime = this.launchStartTime;
      }
      if (this.launchEndTime) {
        data.maxLaunchTime = this.launchEndTime;
      }

      Core.Api.GameAccount.search(data)
        .then(res => {
          let list = res.list
          let result = []
          if (list.length > 0 && list.length % 4 > 0) {
            let num = 4 - (list.length % 4);
            for (let i = 0; i < num; i++) {
              list.push({})
            }
          }
          for (let i = 0; i < list.length / 4; i++) {
            result.push({
              key: i,
              val: list.slice(4 * i, 4 * i + 4)
            })
          }
          this.accountList = result
          this.total = res.count
        })
        .catch(err => {
          this.$Message.error(err.msg);
        })
    },
    pageChange(page) {
      this.currentPage = page
      this.getAccountList()
    },
    selectTag(tag, item) {
      let result = []
      let list = []
      let val = 0
      if (item.map) {
        this.keyList.forEach(v => {
          if (v.key === item.map) {
            val = v.val
          }
        })
      }
      // if (!item.clear) {
      //
      // }
      this.tmpList.forEach(v => {
        if (val > 0) {
          if (v.parentId === item.id && item.id > 0 && val !== v.idx) {
            list.push(v)
          }
        } else {
          if (v.parentId === item.id && item.id > 0) {
            list.push(v)
          }
        }
      })
      this.tagList.forEach(v => {
        v.gameTagInfoList.forEach(t => {
          if (t.id === item.parentId && item.parentId > 0) {
            v.gameTagInfoList[0].clear = true
          }
        })
      })
      this.tagList.forEach(v => {
        let clearTag = false
        if (list.length <= 0 && v.id === val) {
          v.gameTagInfoList = [{
            id: 0,
            name: '全部',
            selected: true,
            parentId: 0
          }]
        }
        for (let i = 0; i < list.length; i++) {
          if (v.id === list[i].idx) {
            list[i].selected = false
            if (!clearTag) {
              clearTag = true
              v.gameTagInfoList = [{
                id: 0,
                name: '全部',
                selected: true,
                parentId: 0
              }]
            }
            v.gameTagInfoList.push(list[i])
          }
        }
      })
      this.tagList.forEach(v => {
        if (v.id === tag.id) {
          v.gameTagInfoList.forEach(t => {
            t.selected = t.id === item.id
          })
        }
      })
      this.tagList.forEach(v => {
        v.gameTagInfoList.forEach(t => {
          if (t.selected && t.id > 0) {
            result.push(t.id)
          }
        })
      })
      this.gameTagInfoList = result
      this.currentPage = 1
      this.getAccountList()
    },
    onClickSearch() {
      this.currentPage = 1
      this.getAccountList()
    },
    goToAccountInfoPage(id) {
      window.open("/account-on-sale/detail?id=" + id, '_blank')
    },
    onClickSort(type) {
      if (type === 'price') {
        this.priceSort = (this.priceSort + 1) % 2;
      } else if (type === 's1') {
        this.s1Sort = (this.s1Sort + 1) % 2;
      } else if (type === 's2') {
        this.s2Sort = (this.s2Sort + 1) % 2;
      } else if (type === 'hot') {
        this.hotSort = (this.hotSort + 1) % 2;
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.account-on-sale-page {
  background: rgb(245, 245, 245);

  .breadcrumb-holder {
    width: 1200px;
    height: 62px;
    margin: auto;

    .item {
      font-weight: 500;
      font-size: 16px;
      color: #A6ACB3;
      cursor: pointer;

      &.active {
        color: #3A3D41;
      }
    }

    .arrow {
      margin: 0 10px;
      width: 16px;
      height: 16px;
    }
  }

  .sec-01 {
    width: 1200px;
    margin: auto;

    .search-box {
      background: #FFFFFF;
      border-radius: 6px;
      font-size: 14px;
      color: #7F8389;
      padding: 0 20px;

      .search-item {
        padding: 13px 0 3px 0;
        border-bottom: 1px dashed #E6E8EB;

        &:last-child {
          border-bottom: none;
        }

        .key {
          width: 58px;
          font-size: 14px;
          color: #7F8389;
          line-height: 34px;
        }

        .val {
          flex-wrap: wrap;

          .tag {
            font-size: 14px;
            color: #3A3D41;
            line-height: 34px;
            height: 34px;
            padding: 0 12px;
            cursor: pointer;
            margin-right: 20px;
            margin-bottom: 10px;

            &.active {
              color: #F58B01;
              background: #FFF2DF;
              border-radius: 4px;
            }
          }
        }
      }
    }

    .range-search-box {
      margin-top: 10px;
      background: #FFFFFF;
      border-radius: 6px;
      padding: 13px 20px 3px 20px;
      flex-wrap: wrap;

      .asc-desc-box {
        border: 1px solid #E6E8EB;
        border-radius: 4px;
        margin-bottom: 10px;

        .item {
          padding: 0 10px;
          height: 34px;
          border-right: 1px solid #E6E8EB;
          cursor: pointer;

          &:last-child {
            border-right: none;
          }

          .name {
            font-size: 14px;
            color: #3A3D41;
          }

          .icon-box {
            margin-left: 4px;

            img {
              width: 6px;
              height: 6px;
            }
          }
        }

        margin-right: 60px;
      }

      .range-box {
        margin-right: 30px;
        margin-bottom: 10px;

        .title {
          font-size: 14px;
          color: #3A3D41;
        }

        .min-max-box {
          .input {
            background: #F4F4F5;
            border-radius: 17px;
            font-size: 14px;
            text-align: center;
            width: 80px;
            height: 34px;
            border: none;
            outline: none;
            padding: 0 8px;
          }

          .split {
            padding: 0 6px;
          }
        }
      }

      .time-range-box {
        margin-bottom: 10px;

        .title {
          font-size: 14px;
          color: #3A3D41;
        }

        .split {
          padding: 0 6px;
        }
      }

      .btn {
        margin-left: 20px;
        background: #F58A01;
        border-radius: 6px;
        font-weight: 500;
        font-size: 14px;
        color: #FFFFFF;
        margin-bottom: 10px;
        padding: 0 16px;
        cursor: pointer;
        line-height: 34px;
      }
    }

    .content-holder {
      width: 100%;

      .card-box {
        margin-top: 20px;
        margin-right: 20px;
        flex: 1;
        background: #FFFFFF;
        border-radius: 6px;
        font-size: 0;
        overflow: hidden;
        cursor: pointer;

        &:last-child {
          margin-right: 0;
        }

        &:hover {
          .img-box img {
            transform: scale(1.2);
          }
        }

        .img-box {
          position: relative;
          height: 200px;
          overflow: hidden;
          font-size: 0;

          img {
            width: 100%;
            height: 200px;
            transition: all 0.6s;
          }

          .hot-box {
            background: rgba(0, 0, 0, .6);
            border-radius: 20px 0 0 20px;
            font-size: 14px;
            color: #FFFFFF;
            position: absolute;
            top: 20px;
            right: 0;
            padding: 0 12px;
            height: 30px;

            img {
              width: 14px;
              height: 14px;
              margin-right: 2px;
            }
          }
        }

        .content-box {
          padding: 0 14px;

          .name-txt {
            margin: 14px 0;
            font-size: 14px;
            color: #3A3D41;
            line-height: 20px;
            height: 60px;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 3;
            line-clamp: 3;
            -webkit-box-orient: vertical;
          }

          .line {
            width: 100%;
            height: 0;
            border-top: 1px dashed #ECECEC;
          }

          .price-time-box {
            height: 40px;

            .price-box {
              font-weight: 500;
              font-size: 20px;
              color: #F0562D;
              line-height: 28px;

              .unit {
                font-size: 13px;
                line-height: 18px;
                margin-right: 4px;
                align-self: flex-end;
                position: relative;
                bottom: 3px;
              }
            }

            .time-box {
              font-size: 12px;
              color: #7F8389;

              img {
                width: 14px;
                height: 14px;
                margin-right: 4px;
              }
            }
          }
        }
      }

      .page {
        padding-top: 40px;
      }
    }

    .empty-holder {
      margin-top: 10px;
      background: #FFFFFF;
      border-radius: 6px;
      height: 400px;

      .empty-box {
        img {
          width: 60px;
          height: 60px;
        }

        .txt {
          font-size: 13px;
          color: #A6ACB3;
          line-height: 18px;
          margin-top: 20px;
        }
      }
    }
  }
}
</style>
